package com.emath.objects;

import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import com.emath.shared.NetUtils;

public class User {
	private int tokens;
	private int skillLevel;
	public String userID = "";
	private String objectState = "";

	public int getSkillLevel() {
		return skillLevel;
	}
	public void setSkillLevel(int skillLevel) {
		this.skillLevel = skillLevel;
	}
	public int getTokens() {
		return tokens;
	}
	public void setTokens(int tokens) {
		this.tokens = tokens;
	}
	public String getUserID() {
		return userID;
	}
	public String getObjectState() {
		return objectState;
	}
	public void setObjectState(String objectState) {
		this.objectState = objectState;
	}
	public void setUserID(String userID) {
		this.userID = userID;
	}

	public void sqlGetUser() throws Exception {
		final String URL = "http://emath-project.appspot.com/emath/emathservicehttp?function=getuser&userID="
				+ this.userID;
		if (this.objectState.equals("working")) {
			return;
		} else {
			this.objectState = "working";
		}
		new Thread(new Runnable() {
			public void run() {
				try {
					HttpClient httpclient = new DefaultHttpClient();
					// Prepare a request object
					HttpGet httpget = new HttpGet(URL);
					// Execute the request
					HttpResponse response;
					response = httpclient.execute(httpget);
					// Get hold of the response entity
					HttpEntity entity = response.getEntity();
					// If the response does not enclose an entity, there is no
					// need
					// to worry about connection release
					if (entity != null) {
						// A Simple JSON Response Read
						InputStream instream = entity.getContent();
						String result = NetUtils
								.convertStreamToString(instream);
						// A Simple JSONObject Creation
						JSONObject json = new JSONObject(result);
						setTokens(json.getInt("tokens"));
						setSkillLevel(json.getInt("skillLevel"));
						// Closing the input stream will trigger connection
						// release
						setObjectState("free");
						instream.close();
						httpclient.getConnectionManager().shutdown();
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	public void sqlSetUser() throws Exception {
		final String URL = "http://emath-project.appspot.com/emath/emathservicehttp?function=setuser&userID="
				+ this.userID + "&tokens=" + this.tokens;
		if (this.objectState.equals("working")) {
			return;
		} else {
			this.objectState = "working";
		}
		new Thread(new Runnable() {
			public void run() {
				try {
					HttpClient httpclient = new DefaultHttpClient();
					// Prepare a request object
					HttpGet httpget = new HttpGet(URL);
					// Execute the request
					// HttpResponse response;
					// response =
					httpclient.execute(httpget);
					// Get hold of the response entity
					setObjectState("free");
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

}
